/**
 * 
 */
package com.deitel.advjhtp1.gui.actions;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JToolBar;

/**
 * @author Aslanbek
 *
 */
public class ActionSample extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private Action sampleAction;
	private Action exitAction;
	
	public ActionSample()
	{
		super("Using Actions");
		
		sampleAction = new AbstractAction() {
			
			/**
			 * 
			 */
			private static final long serialVersionUID = 1L;

			public void actionPerformed(ActionEvent arg0) {
				
				JOptionPane.showMessageDialog(ActionSample.this, "The sampleAction was invoked");
				
				exitAction.setEnabled(true);
			}
		};
		
		sampleAction.putValue(Action.NAME, "Sample Action");
		
		//sampleAction.putValue(Action.SMALL_ICON, new ImageIcon(getClass().getResource("images/Help24.gif")));
		sampleAction.putValue(Action.SMALL_ICON, new ImageIcon());
		
		sampleAction.putValue(Action.SHORT_DESCRIPTION, "A Sample Action");
		
		sampleAction.putValue(Action.MNEMONIC_KEY, new Integer('S'));
		
		exitAction = new AbstractAction() {
			
			/**
			 * 
			 */
			private static final long serialVersionUID = 1L;

			public void actionPerformed(ActionEvent event) {
				
				JOptionPane.showMessageDialog(ActionSample.this, "The exitAction was invoked");
				System.exit(0);
			}
		};
		
		exitAction.putValue(Action.NAME, "Exit");
		
		//exitAction.putValue(Action.SMALL_ICON, new ImageIcon(getClass().getResource("images/EXIT.gif")));
		sampleAction.putValue(Action.SMALL_ICON, new ImageIcon());
		
		exitAction.putValue(Action.SHORT_DESCRIPTION, "Exit Application");
		
		exitAction.putValue(Action.MNEMONIC_KEY, new Integer('x'));
		
		exitAction.setEnabled(false);
		
		JMenu fileMenu = new JMenu("FIle");
		fileMenu.add(sampleAction);
		fileMenu.add(exitAction);
		
		fileMenu.setMnemonic('F');
		
		JMenuBar menuBar = new JMenuBar();
		menuBar.add(fileMenu);
		setJMenuBar(menuBar);
		
		JToolBar toolBar = new JToolBar();
		
		toolBar.add(sampleAction);
		toolBar.add(exitAction);
		
		JButton sampleButton = new JButton();
		sampleButton.setAction(sampleAction);
		
		JButton exitButton = new JButton(exitAction);
		
		JPanel buttonPanel = new JPanel();
		buttonPanel.add(sampleButton);
		buttonPanel.add(exitButton);
		
		Container container = getContentPane();
		container.add(toolBar, BorderLayout.NORTH);
		container.add(buttonPanel, BorderLayout.CENTER);
	}
	
	public static void main(String args[])
	{
		ActionSample sample = new ActionSample();
		sample.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		sample.pack();
		sample.setVisible(true);
	}
}
